package com.xm.servlet;

import com.xm.biz.CartBiz;
import com.xm.biz.GoodsBiz;
import com.xm.biz.impl.CartBizImpl;
import com.xm.po.Cart;
import com.xm.po.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author xie'lin'feng
 * @date 2020-08-19-14:17
 * @description 购物车选中状态购物车
 */
@WebServlet("/isCheck")
public class CartIsCheckServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        CartBiz cartBiz = new CartBizImpl();
        //获取商品id
        Integer id = Integer.valueOf(req.getParameter("id"));
        //获取选中状态
        Integer check = Integer.valueOf(req.getParameter("check"));
        //取出用户
        HttpSession session = req.getSession();
        Users users = (Users) session.getAttribute("user");
        if (check!=null){
            //修改选中状态
            boolean result = cartBiz.isCheck(id,users.getUserId(),check);
            if (result){
                //成功
                //获取购物车选中的数量
                List<Cart> cartByIdChecked = cartBiz.getCartByIdChecked(users.getUserId());
                session.setAttribute("sum",cartByIdChecked);
                session.setAttribute("cartList",cartBiz.getAll(users.getUserId()));
                resp.sendRedirect("shopcart.jsp");
            }
        }
    }
}
